package com.example.userlogin.service;

import com.example.userlogin.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 通知服务
 * 职责：发送各种通知（邮件、短信等）
 */
@Service
public class NotificationService {

    private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);

    /**
     * 发送欢迎通知（异步）
     */
    @Async
    public void sendWelcomeNotification(User user) {
        try {
            logger.info("发送欢迎通知给用户: {}", user.getUsername());
            
            // 模拟发送邮件
            Thread.sleep(1000); // 模拟网络延迟
            
            logger.info("欢迎邮件发送成功: {}", user.getEmail());
            
        } catch (Exception e) {
            logger.error("发送欢迎通知失败: {}, 错误: {}", user.getUsername(), e.getMessage());
        }
    }

    /**
     * 发送密码重置通知
     */
    @Async
    public void sendPasswordResetNotification(User user, String resetToken) {
        try {
            logger.info("发送密码重置通知给用户: {}", user.getUsername());
            
            // 模拟发送密码重置邮件
            Thread.sleep(500);
            
            logger.info("密码重置邮件发送成功: {}", user.getEmail());
            
        } catch (Exception e) {
            logger.error("发送密码重置通知失败: {}, 错误: {}", user.getUsername(), e.getMessage());
        }
    }

    /**
     * 发送安全警告通知
     */
    @Async
    public void sendSecurityAlert(User user, String alertType, String details) {
        try {
            logger.info("发送安全警告给用户: {}, 类型: {}", user.getUsername(), alertType);
            
            // 模拟发送安全警告
            Thread.sleep(300);
            
            logger.info("安全警告发送成功: {}", user.getEmail());
            
        } catch (Exception e) {
            logger.error("发送安全警告失败: {}, 错误: {}", user.getUsername(), e.getMessage());
        }
    }

    /**
     * 发送账户验证通知
     */
    @Async
    public void sendAccountVerificationNotification(User user, String verificationToken) {
        try {
            logger.info("发送账户验证通知给用户: {}", user.getUsername());
            
            // 模拟发送验证邮件
            Thread.sleep(800);
            
            logger.info("账户验证邮件发送成功: {}", user.getEmail());
            
        } catch (Exception e) {
            logger.error("发送账户验证通知失败: {}, 错误: {}", user.getUsername(), e.getMessage());
        }
    }

    /**
     * 发送登录通知
     */
    @Async
    public void sendLoginNotification(User user, String ipAddress, String userAgent) {
        try {
            logger.info("发送登录通知给用户: {}, IP: {}", user.getUsername(), ipAddress);
            
            // 模拟发送登录通知
            Thread.sleep(200);
            
            logger.info("登录通知发送成功: {}", user.getEmail());
            
        } catch (Exception e) {
            logger.error("发送登录通知失败: {}, 错误: {}", user.getUsername(), e.getMessage());
        }
    }
}